﻿using SaaS.Core;
using System;
using System.Linq;
using XCore.Dapper;
using SaaS.Model.Entity.Auths;
using XCore.Encryption;

namespace SaaS.Tool.Services
{
    public class SystemAppService : DapperProvider
    {
        /// <summary>
        /// 添加默认管理员
        /// </summary>
        public void CreateDefaultAdmin()
        {
            using (SaaSDbContext context = new SaaSDbContext())
            {
                if (context.SystemAdmins.Any(c => c.IsDefault))
                {
                    Console.WriteLine("数据库已存在超级管理员用户");
                    return;
                }
                using (IDapperRepository dapper = CreateDapperDatabase(CoreSetting.DefaultConnectionString))
                {
                    var secretKey = MD5Encryption.MD5Salt();
                    dapper.Insert(new SystemAdmin
                    {
                        UserName = "admin",
                        Password = PwdEncryption.Encryption("a123456", secretKey),
                        Name = "超级管理员",
                        IsDefault = true,//超级管理员，忽略所有权限
                        Status = Model.Enums.UserStatus.Normal
                    });

                }

            }
        }
    }
}
